package de.tabuma.ssoclient.logik;

import java.util.*;
import java.util.logging.*;
import de.tabuma.ssoclient.entity.User;

/**
 *
 * @author alefen
 */
public class UserHashFacade {
    private static final Logger log = Logger.getLogger(UserHashFacade.class.getName());
    private static Hashtable<String,List<User>> hash = new Hashtable<String, List<User>>();
    public synchronized static void put(User user, String ssoId) {
        List<User> pList = hash.get(ssoId);
        if(pList==null) {
            pList = new ArrayList<User>();
        }
        log.warning("u1:"+user.getId()+"/"+user.getName());
        for(int i=0;i<pList.size();i++) {
            User p = pList.get(i);
            log.warning(p.getId()+"/"+p.getName());
            if(p.getId().intValue()==user.getId().intValue()) {
                pList.set(i, user);
                hash.put(ssoId, pList);
                return;
            }
        }

        pList.add(user);
        hash.put(ssoId, pList);
    }

    public synchronized static User get(int id, String ssoId) {
        List<User> pList = hash.get(ssoId);
        if(pList==null) {
            return null;
        }
        for(int i=0;i<pList.size();i++) {
            User p = pList.get(i);

            if(p.getId().intValue()==id) {
                return p;
            }
        }
        return null;
    }

    public synchronized static void remove(int id, String ssoId) {
        List<User> pList = hash.get(ssoId);
        if(pList==null) {
            return;
        }
        for(int i=0;i<pList.size();i++) {
            User p = pList.get(i);
            if(p.getId().intValue()==id) {
                pList.remove(i);
                hash.put(ssoId, pList);
                return;
            }
        }
    }

    public synchronized static List<User> getList(String ssoId) {
        return hash.get(ssoId);
    }

    public synchronized static void setList(List<User> pList, String ssoId) {
        hash.put(ssoId, pList);
    }
}
